package com.cypher.encryptionanddecryption.fragment.protocol.kunlun;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.KunLunProtocolUtils;
import com.cypher.encryptionanddecryption.util.VariableUtils;
import com.cypher.encryptionanddecryption.watcher.DecimalDigitsWatcher;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 写状态、温补参数页面
 * @author: 古霸天
 * @date: 2020/12/25
 */
public class WriteStateFragment extends AbstractProtocolFragment {
    /**
     * 工况累计
     */
    private EditText text1;
    /**
     * 设定压力值
     */
    private EditText text2;
    /**
     * 压力补偿选择
     */
    private Spinner spinner1;
    /**
     * 防拆功能
     */
    private CheckBox cb1;
    /**
     * 出厂状态
     */
    private CheckBox cb2;
    /**
     * 温度反补开关
     */
    private CheckBox cb3;

    private final String[] spinner1Arr = {"全补", "正补", "不补", "设定值补偿"};

    /**
     * 单写状态/温补参数
     */
    private RadioButton rb1;
    /**
     * 单写状态（未出厂）
     */
    private RadioButton rb2;
    /**
     * 单写状态（出厂）
     */
    private RadioButton rb3;
    /**
     * 操作对象
     */
    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public WriteStateFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_kunlun_write_state, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化页面
     * @param view
     */
    private void init(View view) {
        // 定义输入框过滤器
        InputFilter[] inputFilters1 = new InputFilter[1];
        inputFilters1[0] = new InputFilter.LengthFilter(5);

        InputFilter[] inputFilter2 = new InputFilter[1];
        inputFilter2[0] = new InputFilter.LengthFilter(7);

        text1 = view.findViewById(R.id.text1);
        text1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        text1.setFilters(inputFilters1);
        text1.addTextChangedListener(new DecimalDigitsWatcher(text1, 3));

        text2 = view.findViewById(R.id.text2);
        text2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        text2.setFilters(inputFilter2);
        text2.addTextChangedListener(new DecimalDigitsWatcher(text2, 3));

        ArrayAdapter adapter1 = new ArrayAdapter<String>(view.getContext(), R.layout.spinner_item, spinner1Arr);
        adapter1.setDropDownViewResource(R.layout.dropdown_stytle);
        spinner1 = view.findViewById(R.id.spinner1);
        spinner1.setAdapter(adapter1);
        spinner1.setSelection(3);

        cb1 = view.findViewById(R.id.cb1);
        cb2 = view.findViewById(R.id.cb2);
        cb3 = view.findViewById(R.id.cb3);

        rb1 = view.findViewById(R.id.rb1);
        rb2 = view.findViewById(R.id.rb2);
        rb3 = view.findViewById(R.id.rb3);
    }

    @Override
    public void fillFragment(String data) {}

    @Override
    public String getHexCode(String rwType) {
        // 如果是只读的，hex码从实体中取
        if (StringUtils.equals("read", rwType)) {
            return VariableUtils.EMPTY;
        }
        // 如果是写数据，需要重新组包
        return  getWriteCode();
    }

    /**
     * 获取表号写入发送码
     * @return
     */
    private String getWriteCode() {
        String txt1 = text1.getText().toString().trim();
        String txt2 = text2.getText().toString().trim();

        if (VariableUtils.isEmpty(txt1, txt2)) {
            Toast.makeText(activity, "数据不能为空", Toast.LENGTH_SHORT).show();
            return VariableUtils.EMPTY;
        }

        int spinner1Index = spinner1.getSelectedItemPosition();
        boolean cb1Checked = cb1.isChecked();
        boolean cb2Checked = cb2.isChecked();
        boolean cb3Checked = cb3.isChecked();

        String data = "";

        String state1 = stateMap1.get(cb1Checked).get(cb2Checked);
        String state2 = stateMap2.get(spinner1Index).get(cb3Checked);

        if (rb1.isChecked()) {
            String txt1Code = HexUtils.doubleToHex(Double.parseDouble(txt1), 1, 8);
            String txt2Code = HexUtils.doubleToHex(Double.parseDouble(txt2), 3, 8);

            data = state1 + state2 + txt1Code + txt1Code + txt2Code;
        } else if (rb2.isChecked() || rb3.isChecked()) {
            if (rb3.isChecked()) {
                state1 = "24";
            }
            data = state1 + state2 + "FFFFFFFFFFFFFFFFFFFFFFFF";
        }

        return KunLunProtocolUtils.getKunLunCommand("10" + data);
    }

    /**
     * 压力补偿选择和温度反补开关状态字节码
     */
    private Map<Integer, Map<Boolean, String>> stateMap2 = stateMap2();
    private Map<Integer, Map<Boolean, String>> stateMap2() {
        Map<Integer, Map<Boolean, String>> map = new HashMap<>();
        Map<Boolean, String> subMap1 = new HashMap<>();
        subMap1.put(true, "30");
        subMap1.put(false, "20");
        map.put(0, subMap1);
        Map<Boolean, String> subMap2 = new HashMap<>();
        subMap2.put(true, "50");
        subMap2.put(false, "40");
        map.put(1, subMap2);
        Map<Boolean, String> subMap3 = new HashMap<>();
        subMap3.put(true, "10");
        subMap3.put(false, "90");
        map.put(2, subMap3);
        Map<Boolean, String> subMap4 = new HashMap<>();
        subMap4.put(true, "70");
        subMap4.put(false, "60");
        map.put(3, subMap4);
        return map;
    }
    /**
     * 防拆功能和出厂状态状态字节码
     */
    private Map<Boolean, Map<Boolean, String>> stateMap1 = stateMap1();
    private Map<Boolean, Map<Boolean, String>>  stateMap1() {
        Map<Boolean, Map<Boolean, String>> map = new HashMap<>();
        Map<Boolean, String> subMap1 = new HashMap<>();
        subMap1.put(true, "24");
        subMap1.put(false, "20");
        map.put(true, subMap1);
        Map<Boolean, String> subMap2 = new HashMap<>();
        subMap2.put(true, "04");
        subMap2.put(false, "00");
        return map;
    }

}
